package com.darwin.darwinDll.core
{
	import com.darwin.darwinDll.Dll;
	
	import flash.utils.Dictionary;
	import flash.utils.getTimer;

	/**
	 * 配置管理类 
	 * @author vanCopper
	 * 
	 */	
	public class Config
	{
		private static var _instance:Config;
		
		public function Config()
		{
			if(_instance == null){
				_instance = this;
			}
			else{throw new Error("实例化单例类出错-ConfigManager");}
		}
		
		public static function getInstance():Config
		{
			if(_instance == null)
			{
				_instance = new Config();
			}
			return _instance;
		}
		
		private var _configComplete:Function;
		public function initXmlConfig(xmlList:Vector.<XML>,completeFunc:Function):void
		{
			_configComplete = completeFunc;
			parseConfigXML(xmlList);
			completeFunc();
		}
		
//		public var loadingList:Vector.<DllItem> = new Vector.<DllItem>();//loading加载列表 weight = -1;
//		public var mainList:Vector.<DllItem> = new Vector.<DllItem>();//预加载文件列表 weight = 0;
		private var _moudleDic:Dictionary = new Dictionary();
		private var _dllItemDic:Dictionary = new Dictionary();
		/**
		 * 解析xml 
		 * @param xmlList
		 * 
		 */		
		private function parseConfigXML(xmlList:Vector.<XML>):void
		{
			for (var i:int = 0; i < xmlList.length; i++) 
			{
				var xml:XML = xmlList[i];
				var xmlType:String = xml.@type;
				var folderUrl:String = "";
				if(xmlType == "ini")
				{
					folderUrl = Dll.getInstance().iniFolderUrl;
				}else if(xmlType == "outini")
				{
					folderUrl = Dll.getInstance().outFolderUrl;
				}
				var moudleList:XMLList = xml.moudle;
				for each (var j:XML in moudleList) 
				{
					var moudle:XML = j;
					var moudleItem:MoudleItem = new MoudleItem();
					moudleItem.moudleName = moudle.@name;
					moudleItem.type = moudle.@type;
					moudleItem.weight = moudle.@weight;
					var itemXML:XMLList = moudle.item;
					var dllItemList:Vector.<DllItem> = new Vector.<DllItem>();
					for each (var k:XML in itemXML) 
					{
						if(k.@language == Dll.getInstance().lang || k.@language == "all")
						{
							var dllItem:DllItem = new DllItem(folderUrl + k.@url,k.@name);
							dllItem.type = k.@type;
							dllItem.flag = k.@flag;
							dllItem.size = k.@size;
							dllItemList.push(dllItem);
							_dllItemDic[dllItem.name] = dllItem;
						}
					}
					if(_moudleDic[moudleItem.moudleName])
					{
						var tempList:Vector.<DllItem> = _moudleDic[moudleItem.moudleName];
						tempList = tempList.concat(dllItemList);
						_moudleDic[moudleItem.moudleName] = tempList;
					}else
					{
						_moudleDic[moudleItem.moudleName] = dllItemList;
					}
				}
			}
		}
		
		/**
		 * 获取模块加载列表 
		 * @param moudleName
		 * @return 
		 * 
		 */		
		public function getMoudleDllItemList(moudleName:String):Vector.<DllItem>
		{
			return _moudleDic[moudleName];
		}
		
		/**
		 * 根据名字获取加载项 
		 * @param keyName
		 * @return 
		 * 
		 */		
		public function getDllItemByName(keyName:String):DllItem
		{
			return _dllItemDic[keyName];
		}
		
		/**
		 * 获取加载类型 
		 * @param keyName
		 * @return 
		 * 
		 */		
		public function getItemTypeByName(keyName:String):String
		{
			var type:String;
			if(keyName && _dllItemDic[keyName])
			{
				type = DllItem(_dllItemDic[keyName]).type;
			}
			return type;
		}
		
		private var _sheetDic:Dictionary = new Dictionary();
		/**
		 * 注册素材索引配置表 
		 * @param sheetData
		 * 
		 */		
		public function registerMatSheet(sheetData:Object):void
		{
			for(var i:String in sheetData) 
			{
				var obj:Object = sheetData[i];
				var key:String = obj["serverId"];
				var subKey:String = obj["subclass"];
				var sheetKey:String = key + "$" + subKey;
				_sheetDic[sheetKey] = obj;
			}
		}
		
		/**
		 * 获取加载项数据 
		 * @param key
		 * @param subKey
		 * @return 
		 * 
		 */		
		public function getMatObj(key:String,subKey:String = ""):Object
		{
			var indexKey:String = key+"$"+subKey;
			return _sheetDic[indexKey];
		}
	}
}

class MoudleItem
{
	public var moudleName:String;
	public var type:String;
	public var weight:String;
}

